<%@ include file="../import_page.jsp" %>
<%
	//login checking
	if(!loginedChecking(session, UsersType.staff)) {
		//user don't login
		//redirect to home page
		response.sendRedirect(URL_HOMEPAGE);
	}

	String unitId = request.getParameter(PAR_UNIT_ID);
	ArrayList<Units> listUnit = null;
	String errMsg = null;
	Units selectUnit = null;
	
	//Check paramter
	if(!isEmptyString(unitId)) {
		//if unit id provide, get unit detail
		try {
			//get unit info from database
			UnitsControl unitCon = new UnitsControl();
			selectUnit = unitCon.getUnit(unitId);
			
			unitCon.closeDBConnection();
			
			session.setAttribute(SESSION_SELECT_UNIT, selectUnit);
			//go to view assignment
			response.sendRedirect(URL_VIEW_ASSIGNMENT);
			//break the others process
			return;
		} catch (Exception err) {
			//prepare to print error message
			errMsg = err.toString();
		}
	}

	//get the list of unit if no error found
	if(isEmptyString(errMsg)) {
		//try to catch the unit selected before
		if(selectUnit == null && selectUnitCheck(session)) {
			selectUnit = (Units)session.getAttribute(SESSION_SELECT_UNIT);
		}
		//get the list of unit and print it
		try {
			//get login users
			Users logined = (Users)session.getAttribute(SESSION_LOGIN_USER);
			//get unit info from database
			UserControl uCon = new UserControl(logined);
			listUnit = uCon.getUserUnit();
			uCon.closeDBConnection();
			
		} catch (Exception err) {
			errMsg = err.toString();
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Select Unit</title>
<style type="text/css">
<!--
.style1 {	font-size: large;
	font-weight: bold;
}
.style2 {font-size: xx-large}
-->
</style>
</head>

<body>
<%	if(selectUnit != null) { %>
<p>You selected unit: <%=selectUnit.getName() %> (<%=selectUnit.getId() %>).</p><br />
<p>To select the other unit, Check on the Unit name</p>
<%} else {%>
<p>Select a target Unit before you take other actions.</p>
<%} %>
<table width="500" height="402" border="1" bordercolor="#CCCCCC" bgcolor="#00FF99">
  <tr>
    <td width="349" height="63"><div align="center"><span class="style1">Unit Selection </span></div></td>
  </tr>
<%
	if(listUnit != null) {
		Iterator<Units> luI = listUnit.iterator();
		while(luI.hasNext()) {
			Units aUnit = luI.next();
%>  
  <tr>
    <td height="65"><a href="<%= response.encodeURL(addParmeterToUrl(URL_SELECT_UNIT, PAR_UNIT_ID, aUnit.getId()))  %>" class="style2"> <%= aUnit.getName() %> (<%= aUnit.getId() %>) </a></td>
  </tr>
<%
		}
	}
	//print error message
	if(!isEmptyString(errMsg)) {
%>
	<tr>
    <td width="349" height="63"><div align="center"><span class="style1"><%= errMsg %></span></div></td>
  </tr>
<%
	}
%>
</table>
</body>
</html>
